package com.example.service.serviceImpl;

import com.example.constants.Constants;
import com.example.mapper.DeviceMapper;
import com.example.pojo.DeviceAttribute;
import com.example.service.DeviceService;
import com.example.utils.GetDeviceAttribute;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.math.BigDecimal;

@Slf4j
@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public Integer getDeviceOnlineStatus(String token) throws Exception {
        String deviceAttribute = GetDeviceAttribute.getDeviceAttribute(Constants.PROJECT_ID, Constants.DEVICE_ID, Constants.DEVICE_URL, Constants.SERVICE_ID, Constants.NODE_ID, token);

        if (deviceAttribute == null || deviceAttribute.isEmpty()) {
            throw new Exception("设备属性获取失败");
        }

        // 使用Jackson解析JSON字符串
        JsonNode jsonNode = objectMapper.readTree(deviceAttribute);
        // 获取设备在线状态
        try{
            String status = jsonNode.get("status").asText();
            if (status.equals("ONLINE")) {
                log.info("设备在线");
                return Constants.DEVICE_ONLINE_STATUS;
            } else if (status.equals("OFFLINE")) {
                log.info("设备离线");
                return Constants.DEVICE_OFFLINE_STATUS;
            }
        } catch (Exception e) {
            throw new Exception("设备属性解析错误");
        }

        return Constants.DEVICE_OFFLINE_STATUS;
    }

    /**
     * 获取设备属性
     */
    @Override
    public DeviceAttribute getAttribute(String token) throws Exception {
        String deviceAttribute = GetDeviceAttribute.getDeviceAttribute(Constants.PROJECT_ID, Constants.DEVICE_ID, Constants.DEVICE_SHADOW_URL, Constants.SERVICE_ID, Constants.NODE_ID, token);
// 使用Jackson解析JSON字符串
        JsonNode jsonNode = objectMapper.readTree(deviceAttribute);

        // 获取设备属性
        JsonNode properties = jsonNode.get("shadow").get(0).get("reported").get("properties");

        //封装到DeviceAttribute对象中
        DeviceAttribute attribute = new DeviceAttribute();
        if (properties.has("Temperature")) {    // 温度
            attribute.setTemperature(BigDecimal.valueOf(properties.get("Temperature").asDouble()));
        }
        if (properties.has("Humidity")) {   // 湿度
            attribute.setHumidity(BigDecimal.valueOf(properties.get("Humidity").asDouble()));
        }
        if (properties.has("Door_State")) {     // 门状态
            attribute.setDoorState("ON".equals(properties.get("Door_State").asText()) ? 1 : 0);
        }
        if (properties.has("Curtain_State")) {  // 窗帘状态
            attribute.setCurtainState("ON".equals(properties.get("Curtain_State").asText()) ? 1 : 0);
        }
        if (properties.has("Air_Quality")) {    // 空气质量
            attribute.setAirQuality(properties.get("Air_Quality").asInt());
        }
        if (properties.has("PPM")) {    // 空气污染平均指数
            attribute.setPpm(properties.get("PPM").asInt());
        }
        if (properties.has("Doorway")) {    // 门口红外检测
            attribute.setDoorway(properties.get("Doorway").asBoolean() ? 1 : 0);
        }
        if (properties.has("Light")) {  // 灯状态
            attribute.setLight("ON".equals(properties.get("Light").asText()) ? 1 : 0);
        }

        return attribute;
    }
}
